package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Czfan;
import com.qianniu.napi.admin.entity.Fenxiao;
import com.qianniu.napi.admin.entity.Jifen;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.penum.Hytype;
import com.qianniu.napi.admin.service.ICzfanService;
import com.qianniu.napi.admin.service.IFenxiaoService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分销设置表
 * </p>
 *
 * @author chenxuebin
 * @since 2019-01-16
 */
@Controller
public class FenxiaoController {

    private static final Logger logger = LoggerFactory.getLogger(FenxiaoController.class);

    @Autowired
    private IFenxiaoService fenxiaoService;
    @Autowired
    private ICzfanService czfanService;

    @RequestMapping("admin/fenxiao/index")
    public String index(Model model) {

        List<Fenxiao> fxlist = fenxiaoService.selectList(new EntityWrapper<Fenxiao>().eq("mpid", ObjectUtil.getMpid()));
        if (fxlist != null) {
            model.addAttribute("fxcout", fxlist.size());
            model.addAttribute("hytypes", initHytypes(fxlist));
        } else {
            model.addAttribute("fxcout", 0);
            model.addAttribute("hytypes", initHytypes(new ArrayList<Fenxiao>()));
        }
        return "admin/fenxiao";
    }

    @RequestMapping("/admin/fenxiao/list")
    @ResponseBody
    public Page<Fenxiao> list(@RequestParam(value = "offset", required = false) Integer offset,
                              @RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "search", required = false) String search,
                              @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Fenxiao> wrapper = new EntityWrapper<Fenxiao>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());

//        if (status != null && status != 99) wrapper.eq("type", status);
//        if (status!=null&&status != 99) wrapper.eq("gtype", status);
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("name", search).or().like("goods_no", search);

        wrapper.orderBy("id", false);
        Page<Fenxiao> page = fenxiaoService.selectPage(new Page<Fenxiao>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/fenxiao/add")
    public String add(@Valid Fenxiao bean, Model model) {

        Fenxiao fenxiao = fenxiaoService.selectOne(new EntityWrapper<Fenxiao>()
                .eq("mpid", ObjectUtil.getMpid())
                .eq("hytype", bean.getHylevel())
        );
        if(fenxiao!=null){
            model.addAttribute("msg", "该会员类型已设置");
            return "admin/fenxiao";
        }

        bean.setCreatetime(new Date());
        bean.setUid(ObjectUtil.getUid());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.insert();

        List<Fenxiao> fxlist = fenxiaoService.selectList(new EntityWrapper<Fenxiao>().eq("mpid", ObjectUtil.getMpid()));
        if (fxlist != null) {
            model.addAttribute("fxcout", fxlist.size());
            model.addAttribute("hytypes", initHytypes(fxlist));
        } else {
            model.addAttribute("fxcout", 0);
            model.addAttribute("hytypes", initHytypes(new ArrayList<Fenxiao>()));
        }

        return "admin/fenxiao";
    }

    @RequestMapping("/admin/fenxiao/getFenxiaoById")
    @ResponseBody
    public Fenxiao getFenxiaoById(long id) {
        Fenxiao fenxiao = fenxiaoService.selectById(id);
        return fenxiao;
    }

    @RequestMapping("/admin/fenxiao/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = fenxiaoService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/fenxiao/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Fenxiao bean = new Fenxiao();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        fenxiaoService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/fenxiao/modify")
    public String update(Fenxiao bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        Fenxiao oldbean = fenxiaoService.selectById(bean.getId());

        boolean isSuccc = fenxiaoService.updateById(bean);
        int level = bean.getHylevel();

        // 只针对用户级设置在线充值会员
        if(bean.getHytype()== Hytype.user.getVal()) {
            for (int i = 1; i < level; i++) {
                Czfan czfan = czfanService.selectOne(new EntityWrapper<Czfan>()
                        .setSqlSelect("id,level,hyname")
                        .eq("mpid", bean.getMpid()).eq("level", i));
                if (czfan != null && i == 1) {
                    czfan.setChong(bean.getHuifei1());
                    czfan.setHyname(bean.getFxname1());
                }
                if (czfan != null && i == 2) {
                    czfan.setChong(bean.getHuifei2());
                    czfan.setHyname(bean.getFxname2());
                }
                if (czfan != null && i == 3) {
                    czfan.setChong(bean.getHuifei3());
                    czfan.setHyname(bean.getFxname3());
                }
                if (czfan != null && i == 4) {
                    czfan.setChong(bean.getHuifei4());
                    czfan.setHyname(bean.getFxname4());
                }
                if (czfan != null && i == 5) {
                    czfan.setChong(bean.getHuifei5());
                    czfan.setHyname(bean.getFxname5());
                }
                if (czfan != null && i == 6) {
                    czfan.setChong(bean.getHuifei6());
                    czfan.setHyname(bean.getFxname6());
                }
                if (czfan != null && i == 7) {
                    czfan.setChong(bean.getHuifei7());
                    czfan.setHyname(bean.getFxname7());
                }
                if (czfan != null) czfan.updateById();
            }
        }

        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }


    private List<Jinfo> initHytypes(List<Fenxiao> fxlist){
        List<Jinfo> result = new ArrayList<>();
        for(int i=1;i<=5;i++){
            boolean exsist = false ;
            for(Fenxiao fenxiao:fxlist){
                if(fenxiao.getHytype()==i){
                    exsist = true;
                }
            }
            if(!exsist){
                if(i==1){
                    Jinfo jinfo = new Jinfo();
                    jinfo.setCode(1);
                    jinfo.setMsg("用户级");
                    result.add(jinfo);
                }
                if(i==2){
                    Jinfo jinfo = new Jinfo();
                    jinfo.setCode(2);
                    jinfo.setMsg("商家级");
                    result.add(jinfo);
                }
                if(i==3){
                    Jinfo jinfo = new Jinfo();
                    jinfo.setCode(3);
                    jinfo.setMsg("代理级");
                    result.add(jinfo);
                }
                if(i==4){
                    Jinfo jinfo = new Jinfo();
                    jinfo.setCode(4);
                    jinfo.setMsg("导购级");
                    result.add(jinfo);
                }
                if(i==5){
                    Jinfo jinfo = new Jinfo();
                    jinfo.setCode(5);
                    jinfo.setMsg("平台级");
                    result.add(jinfo);
                }
            }
        }
        return result;
    }

}
